package com.ruoyi.wvp.controller;

import java.util.List;
import java.util.Date;
import java.util.stream.Collectors;

import com.ruoyi.wvp.domain.DataRoleAddressUnit;
import com.ruoyi.wvp.service.IDataRoleAddressUnitService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.transaction.Transactional;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wvp.domain.DataRole;
import com.ruoyi.wvp.service.IDataRoleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.enums.DeletedEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 部门数据权限Controller
 * 
 * @author ruoyi
 * @date 2024-08-11
 */
@RestController
@RequestMapping("/wvp/dataRole")
@Tag(name = "【部门数据权限】管理")
public class DataRoleController extends BaseController{
    @Autowired
    private IDataRoleService dataRoleService;
    @Autowired
    private IDataRoleAddressUnitService dataRoleAddressUnitService;

    /**
     * 查询部门数据权限列表
     */
    @Operation(summary = "查询部门数据权限列表")
    @PreAuthorize("@ss.hasPermi('wvp:dataRole:list')")
    @GetMapping("/list")
    public TableDataInfo list(DataRole dataRole) {
        Page<DataRole> page = getPage();
        dataRoleService.pageList(page, dataRole);
        return getTableDataInfo(page);
    }
    @GetMapping("/listAll")
    public AjaxResult listAll(DataRole dataRole) {

        List<DataRole> dataRoles = dataRoleService.listAll(dataRole);
        return AjaxResult.success(dataRoles);
    }


    /**
     * 导出部门数据权限列表
     */
    @Operation(summary = "导出部门数据权限列表")
    @PreAuthorize("@ss.hasPermi('wvp:dataRole:export')")
    @Log(title = "部门数据权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void exportAll(HttpServletResponse response,DataRole dataRole){
        List<DataRole> list = dataRoleService.listAll(dataRole);
        ExcelUtil<DataRole> util = new ExcelUtil<DataRole>(DataRole.class);
        util.exportExcel(response, list, "设备数据");
    }

    /**
     * 获取部门数据权限详细信息
     */
    @Operation(summary = "获取部门数据权限详细信息")
    @PreAuthorize("@ss.hasPermi('wvp:dataRole:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        DataRole byId = dataRoleService.getById(id);
        DataRoleAddressUnit dataRoleAddressUnit = new DataRoleAddressUnit();
        dataRoleAddressUnit.setDataRoleId(byId.getId());
        List<DataRoleAddressUnit> dataRoleAddressUnits = dataRoleAddressUnitService.listAll(dataRoleAddressUnit);
        List<Long> addressUnits = dataRoleAddressUnits.stream().filter(d -> null != d.getAddressUnitId()).map(DataRoleAddressUnit::getAddressUnitId)
                .collect(Collectors.toList());
        byId.setAddressUnitIds(addressUnits);
        return success(byId);
    }

    /**
     * 新增部门数据权限
     */
    @Transactional
    @Operation(summary = "新增部门数据权限")
    @PreAuthorize("@ss.hasPermi('wvp:dataRole:add')")
    @Log(title = "部门数据权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DataRole dataRole){
        dataRole.setDeleted(DeletedEnum.NOMAL.getValue());
        dataRole.setCreateBy(getUsername());
        dataRole.setCreateTime(new Date());
        boolean save = dataRoleService.save(dataRole);
        dataRoleAddressUnitService.addAll(dataRole);
        return toAjax(save);
    }

    /**
     * 修改部门数据权限
     */
    @Transactional
    @Operation(summary = "修改部门数据权限")
    @PreAuthorize("@ss.hasPermi('wvp:dataRole:edit')")
    @Log(title = "部门数据权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DataRole dataRole){
         dataRole.setUpdateBy(getUsername());
         dataRole.setUpdateTime(new Date());
        dataRoleAddressUnitService.addAll(dataRole);
        return toAjax(dataRoleService.updateById(dataRole));
    }

    /**
     * 删除部门数据权限
     */
    @Operation(summary = "删除部门数据权限")
    @PreAuthorize("@ss.hasPermi('wvp:dataRole:remove')")
    @Log(title = "部门数据权限", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable( name = "ids" ) Long[] ids) {
        QueryWrapper<DataRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",List.of(ids));
        DataRole dataRole = new DataRole();
        dataRole.setUpdateTime(new Date());
        dataRole.setUpdateBy(getLoginUser().getUser().getUserName());
        dataRole.setDeleted(DeletedEnum.DELETED.getValue());
        return toAjax(dataRoleService.update(dataRole,queryWrapper));
    }

}
